/**
 * 请求日志xboxLog.js
 * 版本号： v1.0.0
 */
$(document).ready(function(){
    //设置时间
    var curr_time = new Date();
    $("#startTime").datetimebox("setValue", getFirstDay(curr_time) + " 00:00:00");
    $("#endTime").datetimebox("setValue", getFirstDay(curr_time) + " 23:59:59");
    var beginTime = $("#startTime").datebox("getValue");
    var endTime = $("#endTime").datebox("getValue");

    $("#xboxLog_dataList").datagrid({
        fit:true,
        fitColumns: true,
        method: "POST",
        queryParams:{startTime:beginTime,endTime:endTime},
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        border:false,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/xboxLog/logList.do',
        idField: 'id',
        striped:true,
        frozenColumns:[[
            {field:'id',checkbox:true}
        ]],
        columns: [
            [
                {field: 'appKey', title: '应用KEY', width: 70, align: 'center', fixed: true},
                {field: 'method', title: '服务方法', width: 200, align: 'left'},
                {field: 'version', title: '服务版本', width: 55, align: 'center', fixed: true},
                {field: 'locale', title: '语言方式', width: 55, align: 'center', fixed: true},
                {field: 'executionType', title: '调用方式', width: 55, align: 'center', fixed: true},
                {field: 'httpAction', title: '请求方式', width: 55, align: 'center', fixed: true},
                {field: 'format', title: '数据格式', width: 55, align: 'center', fixed: true},
                {field: 'duration', title: '请求耗时', width: 55, align: 'center', fixed: true,formatter:function(val){
                        return val / 1000 + " 秒";
                }, fixed: true},
                {field: 'sessionUserName', title: '会话用户', width: 100, align: 'center', fixed: true},
                {field: 'serviceCode', title: '响应代码', width: 55, align: 'center', fixed: true},
                {field: 'executeStatus', title: '调用结果', width: 55, align: 'center', fixed: true, formatter:function(val){
                    if(val == 1) return "<span style='color: green; font-weight: bold'>成功</span>";
                    if(val == 0) return "<span style='color: red; font-weight: bold'>失败</span>";
                }, fixed: true},
                {field: 'serviceCode1', title: '执行结果', width: 55, align: 'center', fixed: true, formatter:function(val,row){
                    if('PC_200' == row.serviceCode){
                        return "<span style='color: green; font-weight: bold'>成功</span>";
                    }else{
                        return "<span style='color: red; font-weight: bold'>失败</span>";
                    }
                }, fixed: true},
                {field: 'callType', title: '应用类型', width: 65, align: 'center', fixed: true},
                {field: 'callVersion', title: '应用版本', width: 55, align: 'center', fixed: true},
                {field: 'client', title: '应用平台', width: 65, align: 'center', fixed: true},
                {field: 'ip', title: '客户端IP', width: 95, align: 'center', fixed: true},
                {field: 'createDate', title: '请求时间', width: 130, align: 'center', fixed: true}
            ]
        ],
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                $("#xboxLog_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#xboxLog_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查看按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            xboxLog_Details($(this).linkbutton("options").iconCls);
                        }
                    });
                }
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            xboxLog_findList();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 查询请求日志
 */
function xboxLog_findList(){
        $("#xboxLog_dataList").datagrid("reload",{
             appKey:$("#appKey").val(),
             method:$("#method").val(),
       sessionPhone:$("#sessionPhone").val(),
    sessionUserName:$("#sessionUserName").val(),
      executeStatus:$("#executeStatus").combobox("getValue"),
        serviceCode:$("#serviceCode").combobox("getValue"),
          startTime:$("#startTime").datetimebox("getValue"),
            endTime:$("#endTime").datetimebox("getValue"),
           callType:$("#callType").combobox("getValue"),
             client:$("#client").val(),
                 ip:$("#ip").val()}
        );
        $("#xboxLog_dataList").datagrid("clearSelections");
}

/**
 * 查看日志详情
 */
function xboxLog_Details(iconCls){
    var row = $("#xboxLog_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的日志详情", timeout: 3000, showType: "slide" });
        return;
    }
    //生成唯一ID
    var id = row.method + row.duration + row.createDate;
    //IFRAM打开窗口
    layoutSub(rzxq.id,rzxq.url + row.id, iconCls, rzxq.closable, rzxq.title);

}

